package com.bdqn.servlet.emp;
import com.bdqn.entity.Employee;
import com.bdqn.entity.ResultInfo;
import com.bdqn.service.CheckWorkService;
import com.bdqn.service.Impl.CheckWorkServiceImpl;
import com.bdqn.util.JSON;
import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@WebServlet("/checkWork")
public class CheckWorkServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String empId = request.getParameter("empId");
        CheckWorkService service = new CheckWorkServiceImpl();
        Date d = new Date();
        String date = new SimpleDateFormat("yyyy-MM-dd").format(d);
        String time = new SimpleDateFormat("HH:mm:ss").format(d);
        /*try {
            Date dd = new SimpleDateFormat().parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }*/
        ResultInfo info = new ResultInfo();
        //判断上班还是下班
        int count = service.findCout(empId,date);
        if(count == 0){
            info.setErrorMsg("上班打卡成功！");
        }else{
            info.setErrorMsg("下班打卡成功！");
        }
        Employee emp = (Employee)request.getSession().getAttribute("emp");
        boolean flag = service.insert(empId, date, time,emp,d);
        info.setFlag(flag);
        JSON.writeAsString(info,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request,response);
    }
}
